package com.hr.emp.controller;

import com.github.pagehelper.PageHelper;
import com.hr.common.retrun.R;
import com.hr.common.utils.EntityConvertUtil;
import com.hr.emp.param.EmpAdd;
import com.hr.emp.param.EmpInfo;
import com.hr.emp.param.EmpPage;
import com.hr.emp.param.EmpVo;
import com.hr.emp.service.EmpService;
import com.hr.system.controller.BaseController;
import com.hr.system.entity.SysUser;
import com.hr.system.enums.SysUserTypeEnums;
import com.hr.system.service.SysDeptService;
import org.simpleframework.xml.Path;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/api/emp")
@CrossOrigin("*")
public class EmpController extends BaseController {
    @Resource
    private EmpService empService;

    @Resource
    private SysDeptService deptService;

    /**
     * 分页查询员工列表
     */
    @GetMapping("/list")
    public R list(@Valid EmpPage empPage) {
        List<EmpVo> voList = new ArrayList<>();
        PageHelper.startPage(empPage.getPageNum(), empPage.getPageSize());

        SysUser sysUser = EntityConvertUtil.convert(empPage, SysUser.class);

        // 搜索员工列表
        sysUser.setType(SysUserTypeEnums.EMP.getCode());
        List<SysUser> empList = empService.getEmpList(sysUser);
        for (SysUser emp : empList) {

            EmpVo empVo = EntityConvertUtil.convert(emp, EmpVo.class);
            if (Objects.nonNull(empVo.getDeptId())) {
                empVo.setDeptName(deptService.selectById(empVo.getDeptId()).getDeptName());
            }
            voList.add(empVo);
        }

        return R.ok(getDataTable(voList, empList));
    }

    /**
     * 根据主键查询员工
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id) {
        return empService.getById(id);
    }

    /**
     * 新增员工
     */
    @PostMapping("/add")
    public R add(@RequestBody @Valid EmpAdd empAdd) {
        if (empAdd.getEntryTime().getTime() > new Date().getTime()) {
            return R.error("入职时间不能大于当前时间");
        }
        return empService.add(empAdd);
    }

    /**
     * 批量删除员工
     */
    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody List<Integer> idList) {
        return empService.batchDelete(idList);
    }

    /**
     * 编辑员工详情
     */
    @GetMapping("/info/{id}")
    public R getEmpInfo(@PathVariable("id") Integer id) {
        return empService.getEmpInfo(id);
    }

    /**
     * 编辑员工信息
     */
    @PostMapping("/edit")
    public R edit(@RequestBody EmpInfo empInfo) {
        return empService.edit(empInfo);
    }

    /**
     * 获取本周打卡记录
     */
    @GetMapping("/getCurWorkRecord/{id}")
    public R getCurWorkRecord(@PathVariable("id") Integer id) {
        return empService.getCurWorkRecord(id);
    }

    /**
     * 获取四周内打卡记录
     */
    @GetMapping("/getCurMonthRecord/{id}")
    public R getCurMonthRecord(@PathVariable("id") Integer id) {
        return empService.getCurMonthRecord(id);
    }

}

